package  com.hq.fxh.No0_ChengQu.Fragment;

/**
 * Created by admin on 2017/8/3.
 */

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hq.fxh.Utils.LogUtils;

/**
 * Created by Administrator on 2016/7/25 0025.
 */
public abstract class BaseFragment extends Fragment {
    protected View rootView;// 缓存Fragment view
    private ProgressDialog progressDialog;
    protected Activity myActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View rootView = null;
        if (rootView == null) {
            rootView = getLayoutView();
            if (rootView == null)
                rootView = inflater.inflate(getLayoutId(), null, false);
            onBeforeSetInject();
            init(savedInstanceState);
            long start = System.currentTimeMillis();
            findView(rootView);
            LogUtils.w(String.format("%s-findView：%s毫秒", getClass().getSimpleName(),
                    (System.currentTimeMillis() - start)));

        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    protected abstract void findView(View view);

    public int getLayoutId() {
        return 0;
    }

    public View getLayoutView() {
        return null;
    }

    /**
     * 在ViewUtils.inject之前执行逻辑
     */
    protected void onBeforeSetInject() {
    }

    protected void init(Bundle savedInstanceState) {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        myActivity = getActivity();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        long start = System.currentTimeMillis();
        initData();
        LogUtils.w(String.format("%s-initData耗时：%s毫秒", getClass().getSimpleName(),
                (System.currentTimeMillis() - start)));

    }

    protected abstract void initData();

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        LogUtils.i("onHiddenChanged : " + hidden);
//        if(hidden){
//            notifyResume();
//        }
//    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (this.isVisible()) {
            // we check that the fragment is becoming visible
            if (isVisibleToUser) {

                notifyResume();
                // async http request here

            }
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    /**
     * 界面可见状态下调用
     * fragment resume
     */
    protected void notifyResume() {

    }

    public void showProgressDialog() {
        showProgressDialog("");
    }

    public void showProgressDialog(String message) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
        }
        progressDialog.setMessage(message);
        progressDialog.show();
    }


    public void dismissDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            afterProgressDismiss();
        }
    }

    /**
     * 加载对话框取消 ,可处理取消请求操作
     */
    protected void afterProgressDismiss() {

    }

}
